import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'LoginPage',
      component: () => import('@/views/LoginPage.vue'),
      meta: { requiresAuth: false },
    },
    {
      path: '/register',
      name: 'RegisterPage',
      component: () => import('@/views/RegisterPage.vue'),
      meta: { requiresAuth: false },
    },
    {
      path: '/403',
      name: 'ForbiddenPage',
      component: () => import('@/views/403Page.vue'),
      meta: { requiresAuth: false },
    },
    {
      path: '/',
      component: () => import('@/layouts/DefaultLayout.vue'),
      children: [
        {
          path: '', // 默认子路由
          name: 'WelcomePage',
          component: () => import('@/views/WelcomePage.vue'),
          meta: {
            requiresAuth: true,
          },
        },
        {
          path: 'users',
          name: 'UserManagement',
          component: () => import('@/views/UserManagement.vue'),
          meta: {
            requiresAuth: true,
            requiresAdmin: true,
          },
        },
        {
          path: 'profile',
          name: 'PersonalPage',
          component: () => import('@/views/PersonalPage.vue'),
          meta: {
            requiresAuth: true,
          },
        },
        {
          path: 'posts',
          name: 'PostManager',
          component: () => import('@/views/PostManager.vue'),
          meta: {
            requiresAuth: true,
            requiresAdmin: true,
          },
        },
        {
          path: 'check',
          name: 'CheckManager',
          component: () => import('@/views/CheckManager.vue'),
          meta: {
            requiresAuth: true,
            requiresAdmin: true,
          },
        },
        {
          path: 'post/:id',
          name: 'PostDetails',
          component: () => import('@/views/PostDetails.vue'),
          meta: {
            requiresAuth: true,
            requiresAdmin: true,
          },
        },
        {
          path: 'reported',
          name: 'ReportedPost',
          component: () => import('@/views/ReportedPost.vue'),
          meta: {
            requiresAuth: true,
            requiresAdmin: true,
          },
        },
        {
          path: 'version',
          name: 'VersionManage',
          component: () => import('@/views/VersionManage.vue'),
          meta: { requiresAuth: true, requiresAdmin: true },
        },
        {
          path: 'files',
          name: 'FileManager',
          component: () => import('@/views/FileManager.vue'),
          meta: {
            requiresAuth: true,
            requiresAdmin: true,
          },
        },
      ],
    },
    {
      path: '/datascreen',
      name: 'DataScreen',
      component: () => import('@/views/dataScreen.vue'),
      meta: { requiresAuth: true, requiresAdmin: true },
    },
    {
      path: '/chinaMap',
      name: 'ChinaMap',
      component: () => import('@/components/chart_components/ChinaMap.vue'),
      meta: { requiresAuth: true, requiresAdmin: true },
    },
    {
      path: '/chinaMap3D',
      name: 'ChinaMap3D',
      component: () => import('@/components/chinaMap3D/index.vue'),
      meta: { requiresAuth: true, requiresAdmin: true },
    },
     {
          path: '/analytics',
          name: 'Analytics',
          component: () => import('@/components/analyTics/Analytics.vue'),
          meta: { requiresAuth: true, requiresAdmin: true },
        },
    
  ],
})

router.beforeEach((to, from, next) => {
  const userRole = localStorage.getItem('userRole')
  const isLoggedIn = !!localStorage.getItem('token')

  // 添加管理员路由白名单
  const adminRoutes = ['UserManagement', 'DataScreen', 'MaterialManagement']

  if (to.meta.requiresAuth && !isLoggedIn) {
    next('/login')
  } else if (adminRoutes.includes(to.name as string) && userRole !== '0') {
    next('/403') // 添加403页面路由
  } else {
    next()
  }
})

router.afterEach(() => {
  NProgress.done()
})

export default router
